#!/usr/bin/env python
# -*- coding: utf-8 -*-
#


from pymongo.connection import Connection
from pymongo.errors import ConnectionFailure
from pymongo import ASCENDING, DESCENDING

def test_one( collection ) :
    print 'running test-one ...'
    collection.insert( { 'url':'AAAAAAA', 'data':'AAAAA11111111111' } )
    collection.insert( { 'url':'AAAAAAA', 'data':'BBBBBBB111111111' } )
    collection.insert( { 'url':'CCCCCCC', 'data':'CCCCCCC111111111' } )
    collection.insert( { 'url':'AAAAAAA', 'data':'DDDDDDD111111111' } )
    collection.insert( { 'url':'EEEEEEE', 'data':'EEEEEEE111111111' } )

    result = collection.find_one( {'url' : 'AAAAAAA' } )
    print result

    result_set = collection.find( {'url' : 'AAAAAAA'} )
    if result_set :
        print 'count=',result_set.count()
        for row in result_set :
            print 'ROW',row

    result = collection.find_one( {'url' : 'ZZZZZZZZ' } )
    if result :
        print result
    else :
        print 'ZZZZZZZZ not FOUND'

    result_set = collection.find( {'url' : 'ZZZZZZZZ'} )
    if result_set :
        print 'count=',result_set.count()
        for row in result_set :
            print 'ROW',row
    else :
        print 'ZZZZZZZZ not FOUND'

    # clear everything
    collection.remove( {} )
    ## test_one()

def test_two( collection ) :
    print 'running test-two ...'
    collection.insert( { 'url':'AAAAAAA', 'data':'AAAAA11111111111', 'ts':123456789001 } )
    collection.insert( { 'url':'AAAAAAA', 'data':'BBBBBBB111111111', 'ts':123456789002 } )
    collection.insert( { 'url':'CCCCCCC', 'data':'CCCCCCC111111111', 'ts':123456789003 } )
    collection.insert( { 'url':'AAAAAAA', 'data':'DDDDDDD111111111', 'ts':123456789004 } )
    collection.insert( { 'url':'EEEEEEE', 'data':'EEEEEEE111111111', 'ts':123456789005 } )

    result_set = collection.find( {'url' : 'AAAAAAA'} )
    if result_set :
        print 'count=',result_set.count()
        for row in result_set :
            print 'ROW',row

    index = collection.ensure_index( [("ts",DESCENDING),] )

    result_set = collection.find( {'url' : 'AAAAAAA'} )
    if result_set :
        print 'count=',result_set.count()
        print result_set.hint( [('ts', DESCENDING)] ).explain()

        for row in result_set.hint( [('ts', DESCENDING)] ) :
            print 'Hinted-ROW',row
    ## test_two()

def test_three( collection ) :
    print 'running test-three ...'
    collection.insert( { 'url':'AAAAAAA', 'data':'AAAAA11111111111', 'ts':123456789001 } )
    collection.insert( { 'url':'AAAAAAA', 'data':'BBBBBBB111111111', 'ts':123456789002 } )
    collection.insert( { 'url':'CCCCCCC', 'data':'CCCCCCC111111111', 'ts':123456789003 } )
    collection.insert( { 'url':'AAAAAAA', 'data':'DDDDDDD111111111', 'ts':123456789004 } )
    collection.insert( { 'url':'EEEEEEE', 'data':'EEEEEEE111111111', 'ts':123456789005 } )

    result_set = collection.find( {'url' : 'AAAAAAA'} )
    if result_set :
        print 'count=',result_set.count()
        for row in result_set.sort( [('ts', DESCENDING)] ) :
            print 'ROW',row

    ## test_three()

def runTest( collection ) :
    test_one( collection )
    test_two( collection )
    test_three( collection )

    # clear everything
    collection.remove( {} )
    ## runTest()


if  __name__ == '__main__' :

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("", "--host", dest="mongo_host", type="string", default='127.0.0.1',
                        help='hostname or ip of mongodb server')
    parser.add_option("", "--port", dest="mongo_port", type="int", default=27017,
                        help='tcp port of mongodb server')

    parser.add_option("-f", "--file", dest="file", type="string",
                        help='path of xml file to be load')
    parser.add_option("", "--filelist", dest="filelist", type="string", 
                        help='path of list of xml file to be load')

    parser.add_option("-l", "--logconf", dest="logconfig", type="string",
                        help='path to logging configure file')

    parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False,
                        help='show debug information')
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
                        help='show detail information')

    (options, args) = parser.parse_args()
    #if not options.mongo_server or not (options.file or options.filelist) :
    #    parser.print_help( sys.stdout )
    #    sys.exit(0)

    if options.logconfig :
        logging.loadConfigure( options.logconfig )

    if options.debug :
        logger = logging.getLogger()
        logger.setLevel(logging.LEVEL_DEBUG)
    
    

    # Make a connection to Mongo.
    try:
        db_connection = Connection(options.mongo_host, options.mongo_port)
    except ConnectionFailure:
        print "fail to connect mongodb on %s" % options.mongo_server
        sys.exit(1)

    dbh = db_connection['testing']
    collection = dbh['dupk']

    collection.create_index( [('url', 1),] )

    runTest( collection )

